package com;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

public class Demo04Druid {
    public static void main(String[] args){
        // 创建一个Properties对象，用于存储配置信息
        Properties properties = new Properties();
        // 通过类加载器获取名为"jdbc.properties"的配置文件的输入流
        InputStream inputStream = Demo04Druid.class.getClassLoader().getResourceAsStream("com/database.properties");
        // 加载配置文件中的属性到Properties对象
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getProperty("jdbc.driver"));
        dataSource.setUrl(properties.getProperty("jdbc.url"));
        dataSource.setUsername(properties.getProperty("jdbc.user"));
        dataSource.setPassword(properties.getProperty("jdbc.password"));
//        alt + enter
        dataSource.setInitialSize(Integer.parseInt(properties.getProperty("jdbc.initSize")));
        dataSource.setMaxActive(Integer.parseInt(properties.getProperty("jdbc.maxActive")));
        dataSource.setMaxWait(Long.parseLong(properties.getProperty("jdbc.maxWait")));
        for (int i = 0; i < 10; i++) {
            DruidPooledConnection connection = null;
            try {
                connection = dataSource.getConnection();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            System.out.println("connection = " + i +"---"+connection);
        }





    }
}
